package com.sanri.test.testspringbootkafka;

import com.sanri.test.testspringbootkafka.proxy.AuditBiz;
import com.sanri.test.testspringbootkafka.proxy.QueueInvocationHandler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.lang.reflect.Proxy;
import java.util.concurrent.CountDownLatch;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProxyTestMain {
    @Autowired
    private QueueInvocationHandler queueInvocationHandler;

    @Test
    public void test() throws InterruptedException {
        AuditBiz auditBiz = (AuditBiz) Proxy.newProxyInstance(ProxyTestMain.class.getClassLoader(), new Class[]{AuditBiz.class}, queueInvocationHandler);
        ListenableFuture<String> echoMsgFuture = auditBiz.echo("回显消息测试");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        echoMsgFuture.addCallback(new ListenableFutureCallback<String>() {
            @Override
            public void onFailure(Throwable ex) {
                ex.printStackTrace();
                countDownLatch.countDown();
            }

            @Override
            public void onSuccess(String result) {
                System.out.println("回显的消息为:"+result);
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }
}
